package com.android.accessibility.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

public class VideoDirFinder extends Thread {

    private static final int WX_DIRLEN = "990f713c1cd281565d2263632d17aeab".length();
    private Context mContext;
    private Handler mHandler;
    private OnFindListener mOnFindListener;
    private ProgressDialog mProgressDialog;

    public VideoDirFinder(Context context) {
        mContext = context;
        mHandler = new Handler(mContext.getMainLooper());
    }

    public void find() {
        List<String> list = Utils.readVideoDir(mContext);
        boolean needFind = false;
        if (list != null && list.size() > 0) {
            File tmpFile = null;
            for (String s : list) {
                tmpFile = new File(s);
                if (!tmpFile.exists()) {
                    needFind = true;
                }
            }
        } else {
            needFind = true;
        }
        if (needFind || true) {
            showSearchProgress();
        }
    }

    private void showSearchProgress() {
        mProgressDialog = ProgressDialog.show(mContext, "信息", "正在查找微信视频目录,请稍后...");
        mProgressDialog.setCancelable(false);
        mProgressDialog.setCanceledOnTouchOutside(false);
        start();
    }

    public void setOnFindListener(OnFindListener l) {
        mOnFindListener = l;
    }

    private List<String> getWxVideoDir() {
        List<String> list = SDUtils.getAllSdcardPath();
        if (list == null) {
            return null;
        }
        List<String> videoDir = new ArrayList<String>();
        File tmpFile = null;
        for (String s : list) {
            tmpFile = new File(s, "tencent/MicroMsg");
            if (tmpFile.exists()) {
                Utils.addAll(videoDir, findObserverPathFixedLength(tmpFile));
            }
        }
        return videoDir;
    }

    private List<String> findObserverPathFixedLength(File weChatdir) {
        if (weChatdir == null) {
            return null;
        }
        if (!weChatdir.exists()) {
            return null;
        }
        File[] wxDir = weChatdir.listFiles();
        if (wxDir == null) {
            return null;
        }
        List<String> pathList = new ArrayList<String>();
        for (File dir : wxDir) {
            if (dir != null && !TextUtils.isEmpty(dir.getName()) && dir.getName().length() == WX_DIRLEN) {
                File tmpFile = new File(dir, "video");
                if (tmpFile.exists()) {
                    pathList.add(tmpFile.getAbsolutePath());
                }
            }
        }
        return pathList;
    }

    /**
     * 通过名字查找目录
     * @param weChatdir
     * @return
     */
    @SuppressWarnings("unused")
    private List<String> findObserverPath(File weChatdir) {
        try {
            return Utils.findGivenameFolder(weChatdir, "video");
        } catch (Exception e) {
        }
        return null;
    }

    public void run() {
        List<String> videoDir = getWxVideoDir();
        endFind(videoDir);
    }

    private void endFind(final List<String> videoDir) {
        Utils.saveVideoDir(mContext, videoDir);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mProgressDialog != null && mProgressDialog.isShowing()) {
                    mProgressDialog.dismiss();
                }
                if (mOnFindListener != null) {
                    mOnFindListener.onFindComplete(videoDir);
                }
            }
        });
    }

    public interface OnFindListener {
        public void onFindComplete(final List<String> videoDir);
    }
}
